<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>授权登录</title>
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
</head>
<style></style>

<body>
  <div id="gzPay">
    <h2>
      微信授权中...
    </h2>
  </div>
  <script type="text/javascript" src="./js/jquery-3.1.1.min.js"></script>
  <script type="text/javascript">
    var baseUrl = "https://bootapi.51bmj.cn";
    var h5Url = "https://m.51bmj.cn";
    var redirectUrl = h5Url + getParam("redirectUrl");

    function getParam(name) {
      let path = location.href;
      var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
      if (reg.test(path))
        return unescape(RegExp.$2.replace(/\+/g, " "));
      return "";
    }

    var code = getParam("code");
    getOpenid();
    //若有传type appId，则为自定义公众号支付；默认为我们的公众号支付
    function getOpenid() {
      $.ajax({
        type: "get",
        //用正式链接，免得测试token覆盖
        url: baseUrl + "/api/openIdByCode?code=" +
          getParam("code"),
        dataType: "json",
        success: function (res) {
          if (res.code == 200) {
            window.location = redirectUrl + "?openId=" + res.result;
          } else {
            alert("获取openId错误");
            window.location = redirectUrl;
          }
        },
        error: function (xhr, type) {
          alert("授权失败");
          window.location = redirectUrl;
        }
      });
    }

  </script>
</body>

</html>
